﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks;

namespace RemotingLibs
{
    public class ServerBase
    {
        private RemotingBase _remoting;
        private readonly object _lockHelper = new object();
        private readonly int _port;

        public ServerBase(int port)
        {
            _port = port;
        }

        #region Tcp信道

        /// <summary>
        /// 利用Tcp信道，可以用于不同计算机之间程序通讯，速度比Http信道快，比Ipc信道慢
        /// </summary>
        /// <returns></returns>
        public RemotingBase GetTcpInstance()
        {
            if (_remoting == null)
            {
                lock (_lockHelper)
                {
                    if (_remoting == null)
                    {
                        TcpServerChannel serverChannel = new TcpServerChannel("Tcp" + _port, _port);
                        ChannelServices.RegisterChannel(serverChannel, false);

                        _remoting = new RemotingBase();
                        RemotingServices.Marshal(_remoting, "TcpRemoting" + _port, typeof(RemotingBase));
                    }
                }
            }

            return _remoting;
        }

        #endregion

        #region Http信道

        /// <summary>
        /// 利用Http信道，可以用于不同的计算机之间的程序通讯，速度比Tcp信道慢
        /// </summary>
        /// <returns></returns>
        public RemotingBase GetHttpInstance()
        {
            if (_remoting == null)
            {
                lock (_lockHelper)
                {
                    if (_remoting == null)
                    {
                        HttpServerChannel serverChannel = new HttpServerChannel("Http" + _port, _port);
                        ChannelServices.RegisterChannel(serverChannel, false);

                        _remoting = new RemotingBase();
                        RemotingServices.Marshal(_remoting, "HttpRemoting" + _port, typeof(RemotingBase));
                    }
                }
            }

            return _remoting;
        }

        #endregion

        #region Ipc信道

        /// <summary>
        /// 利用Ipc信道，只能用于同一台计算机程序之间通讯，通讯速度最快
        /// </summary>
        /// <returns></returns>
        public RemotingBase GetIpcInstance()
        {
            if (_remoting == null)
            {
                lock (_lockHelper)
                {
                    if (_remoting == null)
                    {
                        IpcServerChannel serverChannel = new IpcServerChannel("Ipc");
                        ChannelServices.RegisterChannel(serverChannel, false);

                        _remoting = new RemotingBase();
                        RemotingServices.Marshal(_remoting, "IpcRemoting", typeof(RemotingBase));
                    }
                }
            }

            return _remoting;
        }

        #endregion

    }
}
